package cn.joker.exception;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 统一异常定义，抛出业务异常
 * 聂伟
 * 2022-04-24
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 6237152976831943115L;

    /**
     * 异常码
     */
    private String code;

    /**
     * 异常信息
     */
    private String message;

    public BusinessException(String code, String message) {
        super("[code=" + code + ", msg=" + message + "]");
        this.code = code;
        this.message = message;
    }

    public BusinessException(String code, String msgFormat, Object... args) {
        super("[code=" + code + ", msg=" + String.format(msgFormat, args) + "]");
        this.code = code;
        this.message = String.format(msgFormat, args);
    }

    public BusinessException(BusinessException e, Object... args) {
        super("[code=" + e.getCode() + ", msg=" + String.format(e.getMessage(), args) + "]");
        this.code = e.getCode();
        this.message = String.format(e.getMessage(), args);
    }

    public BusinessException(String message) {
        super("[code=500, msg=" + message + "]");
        this.code = "500";
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
